package CB_C19;

import javafx.util.Properties;
import javafx.io.Storage;
import javafx.io.Resource;
import java.io.InputStream;
import java.io.OutputStream;
import javafx.stage.Alert.*;
import java.io.*;

/** define output properties */
var properties : Properties = new Properties();
/** define input properties */
var properties2 : Properties = new Properties();

/** for Input- and OutputStream*/
var entry: Storage;
var resource : Resource;
var input   : InputStream;
var output  : OutputStream;

/**Effect_Properties Created on 10.11.2009. <br>
 * <br>
 * Save and Load the Properties of the framedrop effect <br>
 * @author Stefan Blumberger
 */
public class Effect_Properties{

    /**Connect to Effect.txt or create a new one
    *  @return void
    */
    public function connect()
    {
        try
        {
            var  entry = Storage
            {
               source: "Effect.txt";
            };
            resource = entry.resource;
        }catch (IOException)
        {
            resource = entry.resource;
        }
    }
    /**call connect() and
    * save properties into outputstream from var properties
    * @exception throw_"can't store properties"
    */
    public function store_properties()
    {
            try
            {
                connect();
                output = resource.openOutputStream(true);
                store_effect();
                properties.store(output);
                output.close();
            }catch(Exception)
            {
                inform("can't store properties");
            }
    }
     /**call connect() and
    * load properties into inputstream from var properties2
    * @exception throw_"can't read properties"
    */
    public function get_properties()
    {
            try
            {
              connect();
              input = resource.openInputStream();
              properties2.load(input);
              get_effect();
              input.close();

            }catch(Exception)
            {
                inform("can't read properties");
            }
    }
   /**  Save properties(effect_on,duration,begin, brightness,strength,random)
    *   into var properties
    *   @excption: throw_Exception_to_function store_properties
    */
    function store_effect()
    {
        try
        {
            properties.put("effect_on","{Main.class_effect.enable_effect.selected}");
            properties.put("duration", "{Main.class_effect.duration.text}");
            properties.put("begin", "{Main.class_effect.begin.text}");
            properties.put("brightness", "{Main.class_brightness.brightness.value as Integer}");
            properties.put("strength", "{Main.class_strength.frames.value as Integer}");
            properties.put("random", "{Main.class_strength.random.selected}");
        }catch(Exception)
        {
            throw Exception;
        }

    }
    /** Get properties(effect_on,duration,begin, brightness,strength,random)
    *   from properties2
    *   @excption: set default values
    */
    function get_effect()
    {
        var brightness: Integer;
        var strength : Integer;
        var random : Boolean;
        var effect_on :Boolean;

        try
        {
            effect_on = Boolean.valueOf(properties2.get("effect_on"));
            brightness = java.lang.Integer.parseInt(properties2.get("brightness"));
            strength = java.lang.Integer.parseInt(properties2.get("strength"));
            random = Boolean.valueOf(properties2.get("random"));
            Main.class_effect.enable_effect.selected = effect_on;
            Main.class_brightness.brightness.value = brightness;
            Main.class_strength.frames.value = strength;
            Main.class_strength.random.selected = random;
            Main.class_effect.duration.text = properties2.get("duration");
            Main.class_effect.begin.text = properties2.get("begin");

        }catch(Exception)
        {
            Main.class_brightness.brightness.value = 100;
            Main.class_strength.frames.value = 0;
            Main.class_strength.random.selected = false;
            Main.class_effect.duration.text = "0";
            Main.class_effect.begin.text = "0";
        }
    }
}